<template>
  <!-- 编辑和新增里的权限分配未完成 -->

  <div class="user_box">
    <!-- 顶部搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.userInput"
          placeholder="根据用户名搜索"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="inputClick">清空</el-button>
        <el-button type="primary" @click="onSerch">搜索</el-button>
      </el-form-item>

      <!-- 新增user -->
      <el-button
        class="addBtn"
        type="primary"
        icon="el-icon-edit"
        @click="dialogBtn('create')"
        >新增权限组</el-button
      >
    </el-form>
    <!-- 新增的表单 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" label-width="100">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="权限分配" :label-width="formLabelWidth">
          <el-tree :data="dataList" show-checkbox :props="defaultProps">
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="addForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 中间分隔 -->
    <div class="m_alert">
      <i class="el-icon-info"></i>
      <div class="m_text">
        共<span>{{ total }}</span
        >条数据
      </div>
    </div>

    <!-- table表单 -->
    <template>
      <el-table
        :default-sort="{ prop: 'date', order: 'descending' }"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="title" label="用户名" width="410">
        </el-table-column>
        <el-table-column prop="update_date" label="日期" sortable width="410">
        </el-table-column>
        <!--右侧标签 默认插槽 -->
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit1"
              @click="editBtn('edit', row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页导航 -->
      <footPage
      class="footpage"
        :total="total"
        :paginationPage="data.page"
        :paginationPagesize="data.pagesize"
        @pageChange="handleCurrentChange"
        @pageSizeChange="handleSizeChange"
      ></footPage>
      <!-- <div class="block">
        <el-pagination
          background
          :current-page="1"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="100"
          :total="total"
          layout=" prev, pager, next, sizes, jumper"
        >
        </el-pagination>
      </div> -->
    </template>
  </div>
</template>

<script>
import footPage from '../components/page-tool.vue'
import { list, add, remove, update, detail } from '@/api/base/permissions.js'
import { parseTime } from '@/utils/index.js'

export default {
  components: {
    footPage
  },
  data () {
    return {
      dataList: [
        {
          id: 1,
          label: '一级 1',
          children: [
            {
              id: 4,
              label: '二级 1-1'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label'
      },

      checkAll: false,
      isIndeterminate: true,

      id: null,
      total: 0,
      dialogType: 'create',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [{ required: true, message: '请填写邮箱', trigger: 'blur' }],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }]
      },

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        title: '',
        permissions: [1101, 1102, 1103]
      },
      formLabelWidth: '100px',

      data: {
        page: 1,
        pagesize: 10

      },
      formInline: {
        userInput: '',
        region: ''
      },
      tableData: []
    }
  },

  created () {
    this.renderList()
  },

  methods: {
    // table的全选
    handleCheckAllChange (val) {
      // this.checkedCities = val ? cityOptions : []
      this.isIndeterminate = false
    },
    handleCheckedCitiesChange (value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.cities.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length
    },
    // dialog 新增用户的功能
    dialogBtn (type) {
      this.dialogType = type
      // if (this.dialogType === 'create') {
      this.dialogFormVisible = true
      // 加个开关 通过type判断 v-if把编辑里的不展示的隐藏
    },
    cancelBtn () {
      this.dialogType = 'create'
      this.clearAddform()
      this.dialogFormVisible = false
    },
    clearAddform () {
      this.form.title = ''
      this.form.province = ''
    },

    // 表单点击确认
    async addForm () {
      try {
        if (this.dialogType === 'create') {
          const data = await add(this.form)
          console.log(data)
        }
        if (this.dialogType === 'edit') {
          await update({ id: this.id, ...this.form })
          this.renderList()

          // console.log(res)
        }
        this.dialogFormVisible = false
        this.$message.success(
          this.dialogType === 'create' ? '创建成功' : '编辑成功'
        )
        this.renderList()
        this.form.title = ''
      } catch (error) {
        this.$message.error('创建失败')
      }
    },

    // 渲染页面
    async renderList () {
      const { data } = await list(this.data)
      console.log(data)
      this.tableData = data.list.filter(item => {
        item.update_date = parseTime(item.update_date)
        return true
      })
      this.tableData = [...data.list]
      this.total = data.counts
    },

    handleSizeChange (val) {
      this.data.pagesize = val
      this.renderList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.renderList()
      console.log(`当前页: ${val}`)
    },

    // 修改按钮
    async editBtn (type, id) {
      this.id = id
      this.dialogType = type
      const { data } = await detail(id)
      console.log(data)
      this.form.title = data.title
      this.dialogFormVisible = true
    },

    // 删除
    async del (id) {
      try {
        // console.log(id)
        await remove(id)
        this.$message.success('删除成功')
        this.renderList()
        // console.log(data)
      } catch (error) {
        this.$message.error('删除失败')
      }
    },

    // 清空搜索框
    inputClick () {
      this.formInline.userInput = ''
      this.renderList()
    },
    // 根据用户名筛选
    onSerch () {
      if (!this.formInline.userInput) return this.$message.error('不能为空')
      const data = this.tableData.filter(item => {
        return item.title === this.formInline.userInput
      })
      this.tableData = data
    }
  },
  computed: {
    dialogTitle () {
      if (this.dialogType === 'create') return '创建权限组'
      if (this.dialogType === 'edit') return '编辑权限组'
      else {
        return '创建权限组'
      }
    }
  }
}
</script>

<style scoped lang="less">
.user_box {
  width: 1178px;
  // height: 417.5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  margin: 20px;
  padding: 20px;
}
.footpage {
  float:right;
}
.addBtn {
  float: right;
  margin-left: 10px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border: 1px solid #dcdfe6;
  font-weight: 500;
}
.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin: 0;
  border-radius: 4px;
  color: #909399;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.edit1 {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  &:hover {
    background-color: #409eff;
    color: #fff;
  }
}
.edit2 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
</style>
